package com.jy.api.controller.clerk;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.api.dao.StoreDao;
import com.jy.api.domain.Order;
import com.jy.api.domain.Store;
import com.jy.api.domain.SwiftPassMicroPayBaseResult;
import com.jy.api.domain.SwiftPassQRCodePayResult;
import com.jy.api.enums.PayTypeEnum;
import com.jy.api.pay.request.PayRequest;
import com.jy.api.util.MyDateUtils;
import com.jy.api.util.OkHttpUtils;
import com.jy.api.util.Payutils;
import com.jy.api.util.UuidUtil;
import com.jy.api.vo.admin.store.StoreVo;
import com.thoughtworks.xstream.XStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * Created by lihu on 2017/11/11.
 */

@Slf4j
@RestController
@RequestMapping(value = "v1/clerk", produces = "application/json;charset=utf-8")
public class ClerkStoreController {


    @Autowired
    StoreDao storeDao;
    private ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    private XStream xstream;

    @Autowired
    public void setXstream(XStream xstream) {
        this.xstream = xstream;
    }


    /**
     * 获取单店店铺信息
     *
     * @param storeId
     * @return
     */
    @RequestMapping(value = "stores/{storeId}", method = RequestMethod.GET)
    public ResponseEntity getStoreInfo(@PathVariable String storeId) {
        //数据校
        try {
            Store store = storeDao.findOne(storeId);
            if (store == null) {
                return new ResponseEntity("店铺信息不存在", HttpStatus.BAD_REQUEST);
            }
            if (MyDateUtils.getStoreExpire(store.getExpireTime()) < new Date().getTime()) {
                return ResponseEntity.badRequest().body("店铺已到期，请续费后进行使用！");
            }
            StoreVo storeVos = new StoreVo();
            BeanUtils.copyProperties(store, storeVos);
            return new ResponseEntity(storeVos, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("getStoreInfo is error =>storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @GetMapping("deal-data")
    public ResponseEntity dealData() {
        List<Store> store = storeDao.findByMchIdIsNotNull();
        List<Store> storeList = new ArrayList<>();
        for (Store s : store) {
            String orderId = UuidUtil.getUuid();
            PayRequest payRequest = new PayRequest(PayTypeEnum.PAY_WX_NATIVE.getCode(), s.getMchId(),
                    orderId, "测试支付",
                    (int) Math.rint(100), "172.17.0.1",
                    "https://prod-api.huoxingy.com" + "/v1/pay/callBack");
            //这里是生成二维码的
            payRequest.setUpSign(objectMapper.convertValue(payRequest, Map.class), s.getKey());
            String requestBody = payRequest.toXML();
            log.info("The request body of QR code payment is:{}", requestBody);
            String xml = "";
            try {
                xml = OkHttpUtils.xmlPost(Payutils.ALI_QR_CODE, payRequest.toXML());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            SwiftPassQRCodePayResult result = (SwiftPassQRCodePayResult) xstream.fromXML(xml, new SwiftPassQRCodePayResult());
            if (!result.isSuccess()) {
                storeList.add(s);
            }
            log.info("The response XML of micro pay is:{}", xml);
        }
        return new ResponseEntity(storeList, HttpStatus.OK);
    }


}
